package cn.doitedu.smart_marketing.querytools;

import cn.doitedu.smart_marketing.beans.ClickhouseQueryRes;
import cn.doitedu.smart_marketing.beans.CountCondition;
import cn.doitedu.utils.ConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/***
 * @author hunter.d
 * @qq 657270652
 * @wx haitao-duan
 * @date 2021/7/22
 **/
public class ClickhouseQuerier {
    Connection conn;

    public ClickhouseQuerier() throws Exception {
        conn = ConnectionUtils.getClickhouseConnection();
    }

    public ClickhouseQueryRes queryCountCondition(CountCondition condition) {

        String sql = "select count(1) from default.yinew_detail where account=? and eventId=? and timeStamp between ? and ?";
        // System.out.println("select count(1) from default.yinew_detail where account="+condition.getAccount()+" and eventId="+condition.getEventId()+" and timeStamp between ? and ?");
        ClickhouseQueryRes clickhouseQueryRes = new ClickhouseQueryRes(500, -1L);
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, condition.getAccount());
            preparedStatement.setString(2, condition.getEventId());
            preparedStatement.setLong(3, condition.getStartTime());
            preparedStatement.setLong(4, condition.getEndTime());

            ResultSet resultSet = preparedStatement.executeQuery();
            Long count = null;
            while (resultSet.next()) {
                count = resultSet.getLong(1);
                //System.out.println("count: " + count);
            }


            clickhouseQueryRes.setCount(count);
            clickhouseQueryRes.setStatus(1);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


        return clickhouseQueryRes;
    }


}
